package com.xia.mail;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.mail.Message;

public class SendMailOverTimeCheck {

	public static String sendMails(Message mailMessage) {
		ExecutorService exec = Executors.newSingleThreadExecutor();
		SendMailOverTimeCheckImplement sendMail = new SendMailOverTimeCheckImplement(
				mailMessage);
		Future<String> future = exec.submit(sendMail);
		exec.shutdown();// 调用 shutdown 拒绝传入任务，然后调用 shutdownNow（如有必要）取消所有遗留的任务：
		try {
			String obj = future.get(30000 * 1, TimeUnit.MILLISECONDS);// 2分钟没发完算超时
		} catch (TimeoutException e) {
			exec.shutdownNow();
			return "overtime";
		} catch (Exception e) {
			exec.shutdownNow();
			return "overtime";
		}
		return "success";
	}
}
